package com.example.newnoke.ui;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.View;

import com.example.newnoke.APP;
import com.example.newnoke.R;
import com.example.newnoke.data.BaseDatas;
import com.example.newnoke.data.UserData;
import com.example.newnoke.databinding.ActivityMimaBinding;
import com.example.newnoke.mvp.presenter.UserPresenter;
import com.example.newnoke.mvp.view.IUserV;
import com.example.newnoke.utils.API;
import com.geya.jbase.baseactivity.BaseDetailsActivity;
import com.geya.jbase.constant.RequestType;
import com.geya.jbase.uiview.ToastUtil;

import java.util.HashMap;

public class MiMaActivity extends BaseDetailsActivity<UserPresenter> implements IUserV {

    private ActivityMimaBinding mBinding;

    @Override
    public UserPresenter newPresenter() {
        return new UserPresenter(this);
    }

    @Override
    public void init() {

        mTitleButton.setTitles("修改密码");
        mTitleButton.setRButtonText("确认修改");
        mTitleButton.setRButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBinding.mima1.getText().toString().isEmpty()) {
                    ToastUtil.showShort("请出输入原密码");
                    return;
                }
                if (mBinding.mima2.getText().toString().isEmpty()) {
                    ToastUtil.showShort("请出输入新密码");
                    return;
                }
                if (mBinding.mima3.getText().toString().isEmpty()) {
                    ToastUtil.showShort("请出再次输入新密码");
                    return;
                }
                if (!mBinding.mima2.getText().toString().equals(mBinding.mima3.getText().toString())){
                    ToastUtil.showShort("两次输入密码不一致");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                map.put("repassword", mBinding.mima1.getText().toString());
                map.put("password", mBinding.mima2.getText().toString());
                mPresenter.accessServers(RequestType.OKGO_POST, API.PAWD, BaseDatas.class, map);
            }
        });
    }

    @Override
    public void setContentView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_mima);
    }


    @Override
    public void setData(UserData.DataBean data) {

    }

    @Override
    public void showMsg(int code, String msg) {
        ToastUtil.showShort(msg);
        if (code == 200) {
            APP.saveUserInfo("");
            Intent intent = new Intent(this, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

}
